package com.hevs.samplewebapp.server.entities;

import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.hevs.samplewebapp.server.json.KeyDeserializer;
import com.hevs.samplewebapp.server.json.KeySerializer;
import com.hevs.samplewebapp.server.json.MD5Deserializer;


@PersistenceCapable
/**
 * This class is the Entity Users, it creates the table User in the Data Base
 *  
 * @author Lionel Rey, Arnaud Durand
 * @date 08.03.12
 *
 */
public class User {


	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	/**
	 * Primary Key of the user
	 */
	private com.google.appengine.api.datastore.Key key;

	@Persistent
	/**
	 * Set of key containing the users granted to see the medical records or the users for he is granted to see
	 */
	private Set<Key> grantedUsers;

	@Persistent
	/**
	 * Login of the user
	 */
	private String login;

	@Persistent
	/**
	 * firstname of the user
	 */
	private String firstName;

	@Persistent
	/**
	 * lastname of the user
	 */
	private String lastName;

	@Persistent
	/**
	 * Password of the user
	 */
	private String password;

	@Persistent
	/**
	 * Email of the user
	 */
	private String email;

	@Persistent
	/**
	 * Credential Level of the user
	 */
	private int credentialLevel;

	//Constructors

	/**
	 * Constructor of the user without parameters
	 */
	public User()
	{

	}

	/**
	 * Constructor of the user with parameters but without the set of granted user
	 * @param login
	 * login of the user
	 * @param password
	 * password of the user
	 * @param email
	 * email of the user
	 * @param credentialLevel
	 * credential level of the user
	 * @param firstName
	 * firstname of the user
	 * @param lastName
	 * lastname of the user
	 */
	public User ( String login, String password, String email, int credentialLevel , String firstName, String lastName)
	{
		this.login=login;
		this.password=password;
		this.email=email;
		this.credentialLevel=credentialLevel;
		this.firstName=firstName;
		this.lastName=lastName;
	}

	/**
	 * Constructor with all parameters
	 * @param login
	 * login of the user
	 * @param password
	 * password of the user
	 * @param email
	 * email of the user
	 * @param credentialLevel
	 * credential level of the user
	 * @param firstName
	 * firstname of the user
	 * @param lastName
	 * lastname of the user
	 * @param grantedUsers
	 * set of key granted by the user
	 */
	public User ( String login, String password, String email, int credentialLevel , String firstName, String lastName, Set<Key> grantedUsers)
	{
		this.login=login;
		this.password=password;
		this.email=email;
		this.credentialLevel=credentialLevel;
		this.firstName=firstName;
		this.lastName=lastName;
		this.grantedUsers=grantedUsers;
	}

	/**
	 * Getter for the key
	 * @return
	 * user's key
	 */
	@JsonSerialize(using = KeySerializer.class)
	public Key getKey() {
		return key;
	}

	/**
	 * Setter for the key
	 * @param key
	 * user's key to set
	 */
	@JsonDeserialize(using = KeyDeserializer.class)
	public void setKey(Key key) {
		this.key = key;
	}

	@JsonIgnore
	@Deprecated
	public String getId() {
		if(key==null)
			return null;
		return KeyFactory.keyToString(this.key);
	}

	//only for the mock (no generated IDs)
	@JsonIgnore
	@Deprecated
	public void setId(String encoded){
		this.key=KeyFactory.stringToKey(encoded);
	}

	/**
	 * Getter for the login
	 * @return
	 * user's login
	 */
	public String getLogin() {
		return login;
	}

	/**
	 * Setter for the login
	 * @param login
	 * user's login to set
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * Getter for the Password
	 * @return
	 * user's password
	 */
	@JsonIgnore
	public String getPassword() {
		return password;
	}

	/**
	 * Setter for the passwor 
	 * @param password
	 * user's password to set
	 */
	@JsonProperty
	@JsonDeserialize(using = MD5Deserializer.class)
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Getter for the firstname
	 * @return
	 * user's firstname
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * Setter for the firstname
	 * @param firstName
	 * user's firstname
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * Getter for the lastname
	 * @return
	 * user's lastname
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * Setter for the lastname
	 * @param lastName
	 * user's lastname to set
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * Getter for the email
	 * @return
	 * 
	 * uesr's email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Setter for the email
	 * @param email
	 * user's email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Method toString the user
	 */
	@Override
	public String toString() {
		return login + " " + password + " " + email;
	}

	/**
	 * Getter for credentialLevel
	 * @return
	 * user's credential level
	 */
	public int getCredentialLevel() {
		return credentialLevel;
	}

	/**
	 * Setter for the credentialLevel
	 * @param credentialLevel
	 * user's credential level to set
	 */
	public void setCredentialLevel(int credentialLevel) {
		this.credentialLevel = credentialLevel;
	}

	/**
	 * Getter for grantedUsers
	 * @return
	 * user's granted users
	 */
	@JsonIgnore
	public Set<Key> getGrantedUsers() {
		return grantedUsers;
	}

	/**
	 * Setter for grantedUsers
	 * @param grantedUsers
	 * user's granted users Set to set
	 */
	@JsonIgnore
	public void setGrantedUsers(Set<Key> grantedUsers) {
		this.grantedUsers = grantedUsers;
	}
}
